package com.ljh.cloudmusic.controller;

import com.ljh.cloudmusic.model.User;
import com.ljh.cloudmusic.service.UserService;
import com.ljh.cloudmusic.utils.ResponseBodyMessage;
import jakarta.mail.MessagingException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;
import java.util.Random;

/**
 * @author: Themberfue
 * @date: 2025/3/6 21:34
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/login")
    public ResponseBodyMessage<String> login(@RequestBody Map<String, String> data) {
        String username = data.get("username");
        String password = data.get("password");
        return userService.login(username, password);
    }

    @RequestMapping("/register")
    public ResponseBodyMessage<String> register(@RequestBody Map<String, String> data) {
        String username = data.get("username");
        String password = data.get("password");
        return userService.register(username, password);
    }

    @RequestMapping("/getUserInfo")
    public ResponseBodyMessage<User> getUserInfo(HttpServletRequest request) {
        return userService.getUserInfo(request);
    }

    @RequestMapping("/updateUserInfo")
    public ResponseBodyMessage<String> updateUserInfo(@RequestBody Map<String, Object> data, HttpServletRequest request) {
        return userService.updateUserInfo(data, request);
    }

    @RequestMapping("/updatePassword")
    public ResponseBodyMessage<Boolean> updatePassword(@RequestBody Map<String, Object> data, HttpServletRequest request) {
        Integer uuid = (Integer) data.get("uuid");
        String oldPassword = (String) data.get("oldPassword");
        String newPassword = (String) data.get("newPassword");
        return userService.updatePassword(uuid, oldPassword, newPassword, request);
    }

    @RequestMapping("/updateAvatar")
    public ResponseBodyMessage<Boolean> updateAvatar(@RequestParam("uuid") Integer uuid,
                                                     @RequestParam("avatarFile") MultipartFile avatarFile,
                                                     HttpServletRequest request) throws IOException {
        return userService.updateAvatar(uuid, avatarFile, request);
    }


    @RequestMapping("/getAvatar")
    public ResponseEntity<Resource> getAvatar(@RequestParam String path) {
        return userService.getAvatar(path);
    }

    @RequestMapping("/sendCode")
    public ResponseBodyMessage<Boolean> sendCode(@RequestBody Map<String, String> data, HttpServletRequest request) throws MessagingException {
        String email = data.get("email");
        String code = String.valueOf(new Random().nextInt(900000) + 100000); // TODO 随机生成6位验证码
        return userService.sendCode(email, code, request);
    }

    @RequestMapping("/loginByEmailCode")
    public ResponseBodyMessage<String> loginByEmailCode(@RequestBody Map<String, String> data) {
        String code = data.get("code");
        String email = data.get("email");
        return userService.loginByEmailCode(code, email);
    }

    @RequestMapping("/registerByEmailCode")
    public ResponseBodyMessage<String> registerByEmailCode(@RequestBody Map<String, String> data) {
        String code = data.get("code");
        String email = data.get("email");
        return userService.registerByEmailCode(code, email);
    }

    @RequestMapping("/getEmailByUsername")
    public ResponseBodyMessage<String> getEmailByUsername(@RequestBody Map<String, String> data) {
        String username = data.get("username");
        return userService.getEmailByUsername(username);
    }

    @RequestMapping("/updateNewPassword")
    public ResponseBodyMessage<String> updateNewPassword(@RequestBody Map<String, String> data) {
        String username = data.get("username");
        String newPassword = data.get("password");
        return userService.updateNewPassword(username, newPassword);
    }
}
